package com.coders.activities;

import org.cocos2d.nodes.CCDirector;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.coders.R;

public class GameOver extends BaseActivity {

	ImageView playagain, home;
	String[] columns;
	int Score = 0;
	String TOTALSCORE = "score";
	String PLAYERNAME = "playername";

	SQLiteDatabase db;

	SharedPreferences sharedPreference = CCDirector.sharedDirector()
			.getActivity().getSharedPreferences("sharedPreference", 0);

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.gameover);

		int totalscore = getIntent().getIntExtra("totalscore", -1);
		SharedPreferences sharedPreference = CCDirector.sharedDirector()
				.getActivity().getSharedPreferences("sharedPreference", 0);
		long temp_totalscore = (long) totalscore;
		try {
			String bp_totalscore = sharedPreference.getString(TOTALSCORE, null);
			String bp_playername = sharedPreference.getString(PLAYERNAME, null);
			if (totalscore > Integer.parseInt(bp_totalscore)) {

				showSettingsDialog(temp_totalscore);

			}

		} catch (Exception e) {
			showSettingsDialog(temp_totalscore);
		}

		intiatizeviews();
		viewlisteners();

	}

	private void showSettingsDialog(long totalscore) {
		EnterHighscore dialog = new EnterHighscore(this, totalscore);

		dialog.show();

	}

	private void intiatizeviews() {
		playagain = (ImageView) findViewById(R.id.playagainimageView);
		home = (ImageView) findViewById(R.id.homeimageView);

	}

	private void viewlisteners() {

		home.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent start = new Intent(GameOver.this, MainMenu.class);
				startActivity(start);
				finish();
			}
		});

		playagain.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent start = new Intent(GameOver.this, Play.class);
				startActivity(start);
				finish();
			}
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Intent start = new Intent(GameOver.this, MainMenu.class);
		startActivity(start);
		finish();
		return super.onKeyDown(keyCode, event);
	}

}
